package com.dp.appi.service.impl;

import com.dp.appi.dao.ChannelDao;
import com.dp.appi.dao.PTDao;
import com.dp.appi.exception.CommonException;
import com.dp.appi.service.ChannelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * Created by bchen on 2016/5/17.
 */
@Service
@Transactional(rollbackFor=Exception.class)
public class ChannelServiceImpl implements ChannelService{
    @Autowired
    private ChannelDao channelDao;

    @Autowired
    private PTDao pTDao;


    /**
     * 通道特征点(有下级)列表
     *
     * @param exploration_id
     * @return
     */
    @Override
    public List<Map<String, Object>> channelAList(Long exploration_id) {
        return channelDao.channelAList(exploration_id);
    }

    /**
     * 通道特征点（无下级）列表
     *
     * @param exploration_id
     * @return
     */
    @Override
    public List<Map<String, Object>> channelList(long exploration_id) {
        return channelDao.channelList(exploration_id);
    }

    /**
     * 关联通道特征信息
     *
     * @param exploration_id 特征点踏勘id
     * @param after_exp      下级级联
     * @return
     */
    @Override
    public Long corrChannelFeature(long exploration_id, long after_exp) {
        return channelDao.corrChannelFeature(exploration_id,after_exp);
    }

    /**
     * 新增通道特征信息
     *
     * @param pt_id
     * @param afterExp
     * @param distance
     * @return
     */
    @Override
    public Long addChannelFeature(int pt_id, float distance ,Map<String,Object> afterExp) {
        if(null == afterExp){
            throw new CommonException("EXPO002");
        }

        //插入特征信息
        afterExp.put("distance",distance);
        afterExp.put("pt_id",pt_id);
        Long exploration_id = channelDao.addPexploration(afterExp);

        //关联通道特征信息
        return channelDao.corrChannelFeature(exploration_id,Long.parseLong(String.valueOf(afterExp.get("exploration_id"))));
    }

    /**
     * 查询特征点扩展信息
     *
     * @param channel_id
     */
    @Override
    public Map<String, Object> queryChannelFeature(long channel_id) {
        return channelDao.queryChannelFeature(channel_id);
    }

    /**
     * 删除特征点信息
     *
     * @param channelId
     */
    @Override
    public void delChannelFeature(Long channelId) {
        //获取踏勘特征点信息
        Map<String,Object> expMap = channelDao.queryExpByChannelId(channelId);

        Object ptTableObj = expMap.get("pt_table");
        Object objectidObj = expMap.get("pt_objectid");
        Object expInput = expMap.get("exp_input");
        if(expInput == 0){
            if(null!=objectidObj&&null!=ptTableObj){
                String pt_table = String.valueOf(ptTableObj);
                Long pt_objectid = Long.parseLong(String.valueOf(objectidObj));
                //删除特征点扩展信息
                pTDao.delPt(pt_table,pt_objectid);
            }
            //删除踏勘特征点信息
            channelDao.delExploration(Long.parseLong(String.valueOf(expMap.get("exploration_id"))));
        }
        //删除通道属性信息
        channelDao.delChannelFeature(channelId);
    }
}
